package edu.spec.sms.service.impl;

import edu.spec.sms.dao.iface.*;
import edu.spec.sms.model.Clazz;
import edu.spec.sms.model.Student;
import edu.spec.sms.model.mental.Exam;
import edu.spec.sms.model.mental.ExamClazz;
import edu.spec.sms.model.mental.Mental;
import edu.spec.sms.model.mental.StudentMental;
import edu.spec.sms.service.iface.IExamService;
import org.madgecko.basic.model.Pager;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;

@Service
public class ExamService implements IExamService {

    @Inject
    private IExamDao examDao;
    @Inject
    private IClazzDao clazzDao;
    @Inject
    private IExamClazzDao examClazzDao;
    @Inject
    private IStudentDao studentDao;
    @Inject
    private IStudentMentalDao studentMentalDao;

    @Override
    public void add(Exam e) {
        examDao.add(e);
    }

    @Override
    public void delete(Integer examId) {
        examDao.delete(examId);
    }

    @Override
    public void update(Exam e) {
        examDao.update(e);
    }

    @Override
    public Exam load(Integer examId) {
        return examDao.load(examId);
    }

    @Override
    public Pager<Exam> find() {
        return examDao.find();
    }

    @Override
    public void addClazz(Integer examId, Integer clazzId) {
        // 构造ExamClazz
        Exam e = examDao.load(examId);
        Mental m = e.getMental();
        Clazz c = clazzDao.load(clazzId);
        ExamClazz ec = new ExamClazz();
        ec.setExam(e);
        ec.setClazz(c);

        // 增加ExamClazz
        examClazzDao.add(ec);

        // 完成clazz里的学生Student和Mental的关联
        List<Student> lStu = studentDao.listStudentByClazz(clazzId);
        for(Student s : lStu){
            StudentMental sm = new StudentMental(s, m, e.getCloseDate());
            studentMentalDao.add(sm);
        }
    }

    @Override
    public void deletelClazz(Integer examId, Integer clazzId) {
        //依次删除StudentMental、ExamClazz
        //TODO 修正为ExamStudent
//        studentMentalDao.deleteByMental(examId);
        examDao.deletelClazz(examId, clazzId);
    }

    @Override
    public List<Integer> listClazzIdsByExam(Integer examId) {
        return examDao.listClazzIdsByExam(examId);
    }

}
